<template>
	<view class="redbox">
		<view class="redMask"></view>

		<!--发送优惠券的弹窗开始-->
		<view class="red_model" :style="{height:bodyH}" v-if="rbagmodelshow==1">

			<view class="youhuiquan animation-slide-top">

				<!-- <icon type="cancel" color="red" size="28" class="cancel-coupon" @click="cancelCoupon" /> -->
				<icon type="cancel" color="red" size="28" class="cancel-coupon" @click="getCoupon(couponData.id)" />
				<image style="margin-top: 3vh;" :src="couponData.image_url" mode="widthFix"></image>


				<button class="cu-btn  margin-tb-sm lg" style="background-color: #FED201;color: black;width: 80%;"
					@click="getCoupon(couponData.id)"> 确 定 </button>



			</view>

		</view>

		<!--发送优惠券的弹窗的结束-->


	</view>
</template>

<script>
	import countup from '@/components/countUp.vue';
	export default {
		name: 'couponVue',
		components: {
			countup
		},
		data() {
			return {
				num: 152248.68,
				rbagmodelshow: 0,
				openbrnanimation: {},
				rbag_conbg: this.Reques.resDomain + "touxiang.png",
				bodyH: this.WH - this.CustomBar + 'px',
				couponData: {}, //存储优惠券的数据
			}


		},
		onReady() {


			// setTimeout(() => {
			// 	let coupon = (this.$coupon != undefined) ? this.openRed() : false;
			// 	console.log(this.$coupon);
			// }, 2000);

		},
		mounted() {


			console.log("rbagmodelshow " + JSON.stringify(this.$coupon));
		},
		methods: {
			/**
			 * 发放优惠券
			 * @param {Object} dat
			 */
			openRed() { //status 0 1 2
				console.log("优惠券" + JSON.stringify(this.$coupon));
				setTimeout(() => {
					let send = () => {
							this.couponData = (this.$coupon.length == 0) ? [] : this.$coupon[0];
							(this.$coupon.length == 0 && this.$coupon[0].status == 0) ? false: (this
								.rbagmodelshow = 1);

						}
						(this.$coupon != undefined) ? send() : false;
				}, 2000);

			},
			/**
			 * 关闭优惠券的弹窗
			 */
			cancelCoupon() {
				console.log("点击关闭");
				this.rbagmodelshow = 0;

			},
			/**
			 * 领取优惠券
			 * 
			 */
			getCoupon(id) {

				// uni.showLoading({
				// 	title: '请求中...'
				// });
				let dat = {
					action: "receiveDiscount",
					openId: this.Reques.sessionData.data.openId,
					discountId: id
				}
				let tishi = () => {
					// uni.hideLoading();
					this.cancelCoupon();
					// setTimeout(() => {
					// 	this.cancelCoupon();
					// }, 1000);
				}
				this.Reques.commonQuest("xcx/discount.api.php", "POST", dat).then((res) => {
					console.log("请求优惠券 "+JSON.stringify(res));
					if (res.statusCode == 200 && res.data.code == 0) {
						//this.Reques.showToast("领取成功");
						tishi();
					} else {
						///this.Reques.showToast("领取失败，请稍后重试");
						tishi();
					}
				}, err => {
					this.Reques.tishi();
					tishi();
				});











			}




		}



	}
</script>

<style scoped>
	@import "redPack.css";
</style>
